package shuashuami.hb.com.fragment.agent;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import shuashuami.hb.com.avtivity.ASettingActivity;
import shuashuami.hb.com.avtivity.agent.ClickerManagerAct;
import shuashuami.hb.com.avtivity.agent.DistributionCenterAct;
import shuashuami.hb.com.avtivity.agent.UserInfoAct;
import shuashuami.hb.com.avtivity.agent.WodeyuEActivity;
import shuashuami.hb.com.entity.agent.User;
import shuashuami.hb.com.fragment.AbFragment;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.glid.GlidUtil;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.manager.agent.UserManager;

/**
 * <b>类   名：</b>${TYPE_NAME} <br/>
 * <b>类描述：</b> <br/>
 * <b>创建人：</b>zhangqi<br/>
 * <b>创建时间：</b>2017/6/13<br/>
 * <b>修改人：</b>zhangqi<br/>
 * <b>修改时间：</b>2017/6/13<br/>
 * <b>修改备注：</b><br/>
 *
 * @version 1.0.0<br/>
 */
public class UserFragm extends AbFragment {
    RelativeLayout a_personInfo;

    LinearLayout lin_manager, lin_distribute, lin_money, auser_lin_money;

    ImageView img_head;

    TextView user_head_title;

    TextView user_money;

    TextView user_id;

    private GlidUtil glidUtil;

    private TextView txt_distributecenter;

    @Override
    public void initView(View v) {
        setTitleBar("我的", false);
        setRightView();
        a_personInfo = (RelativeLayout) findViewById(R.id.a_personInfo);
        lin_distribute = (LinearLayout) findViewById(R.id.auser_lin_money);
        lin_manager = (LinearLayout) findSelfViewById(R.id.auser_lin_manager);
        lin_money = (LinearLayout) findSelfViewById(R.id.auser_lin_money);
        img_head = (ImageView) findSelfViewById(R.id.user_head);
        user_head_title = (TextView) findSelfViewById(R.id.user_head_title);
        user_money = (TextView) findSelfViewById(R.id.user_money);
        user_id = (TextView) findSelfViewById(R.id.user_id);
        txt_distributecenter = (TextView) findViewById(R.id.txt_distributecenter);
        auser_lin_money = (LinearLayout) findSelfViewById(R.id.auser_lin_money);
    }

    @Override
    public void initData() {
        glidUtil = new GlidUtil(getAbActivity());

    }

    @Override
    public void initListener() {
        setRightViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ASettingActivity.class);
                startActivity(intent);
            }
        });
        a_personInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAbActivity().openActivity(UserInfoAct.class);
            }
        });

        lin_manager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAbActivity().openActivity(ClickerManagerAct.class);
            }
        });

        txt_distributecenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAbActivity().openActivity(DistributionCenterAct.class);
            }
        });
        auser_lin_money.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAbActivity().openActivity(WodeyuEActivity.class);
            }
        });
    }

    @Override
    public int bindView() {
        return R.layout.frag_auser;
    }


    // 最好在登录的时候搞定
    public void getUserInfo() {
        HttpOnNextListener<User> listener = new HttpOnNextListener<User>() {
            @Override
            public void onNext(User user) {
                // 保存个人信息
                UserManager.getInstance().setUser(user);

                getAbActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 刷新ui
                        refershUI();
                    }
                });

            }
        };
        User user = UserManager.getInstance().getUser();
        HttpAgentMethods.getUserInfo(listener, user.getId());
    }


    public void refershUI() {
        User user = UserManager.getInstance().getUser();
        // 修改头像
        //@mipmap/ssm_icon_11
        glidUtil.uploadCircle(user.getAvatar(), img_head, R.mipmap.ssm_icon_11);

//        Glide.with(this).load(user.getAvatar())
//                .transform(new GlideCircleTransform(getContext()))
//                .into(img_head);
        // 修改名称
        user_head_title.setText(user.getUsername());
        // 修改id号
        user_id.setText("ID:" + user.getId());
        // 修改租金
        user_money.setText(user.getMoney() + "");

    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }
}
